package com.wantupai.mvp.framework.lce.impl.activity;

import android.support.annotation.NonNull;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;


import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.wantupai.mvp.R;
import com.wantupai.mvp.framework.MvpBasePresenter;
import com.wantupai.mvp.framework.base.adapter.BaseRecyclerAdapter;
import com.wantupai.mvp.framework.base.decoration.impl.Horizontaion_PaddingLeft_Right_DividerItemDecoration;
import com.wantupai.mvp.framework.lce.MvpLceView;

import java.util.List;


public abstract class BaseRefreshLceActivity<Item, Adapter extends BaseRecyclerAdapter<Item>, D, V extends MvpLceView<D>, P extends MvpBasePresenter<V>> extends BaseMvpLceActivity<D, V, P>
{

    private RecyclerView recyclerView;
    private SmartRefreshLayout xRefreshView;
    private Adapter recyclerAdapter;
    private boolean isDownRefresh = true;

    public RefreshLayout getxRefreshView()
    {
        return xRefreshView;
    }

    @Override
    public void initContentView(View contentView)
    {
        super.initContentView(contentView);
        xRefreshView = (SmartRefreshLayout) contentView.findViewById(R.id.xrefreshview);
        recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
        if (hasItemDecoration())
        {
            recyclerView.addItemDecoration(getDividerItemDecoration());
        }
        recyclerView.setLayoutManager(getLayoutManager());
        recyclerView.setHasFixedSize(false);
        //设置是否自动刷新(进入页面自动刷新)
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerAdapter = bindAdapter();
        recyclerView.setAdapter(recyclerAdapter);
        xRefreshView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener()
        {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout)
            {
                refreshDate(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout)
            {
                refreshDate(true);
            }
        });
    }

    public final boolean isDownRefresh()
    {
        return isDownRefresh;
    }

    public void refreshDate(boolean isDownRefresh)
    {
        this.isDownRefresh = isDownRefresh;
        loadData(true);
    }

    @Override
    public void showError()
    {
        super.showError();
    }

    @Override
    public void bindData(D data)
    {
        super.bindData(data);
        showContent();
        getxRefreshView().setEnableRefresh(true);
        getxRefreshView().setEnableLoadMore(true);
        if (isDownRefresh())
        {
            getxRefreshView().finishRefresh();
        }
        else
        {
            getxRefreshView().finishLoadMore();
        }
        getAdapter().refreshAdapter(isDownRefresh(), convert(data));
    }
    public void refresh()
    {
        int count = getAdapter().getCount();
        if (count > 0)
        {
            getRecyclerView().post(() ->
            {
                getRecyclerView().scrollToPosition(0);
                getxRefreshView().autoRefresh();
            });
        }
        else
        {
            initData();
        }
    }
    @Override
    public void refreshComplete()
    {
        super.refreshComplete();
        getxRefreshView().finishRefresh();
    }

    public abstract List<Item> convert(D d);

    public abstract Adapter bindAdapter();

    public RecyclerView.LayoutManager getLayoutManager()
    {
        return new LinearLayoutManager(this);
    }

    public RecyclerView.ItemDecoration getDividerItemDecoration()
    {
        return new Horizontaion_PaddingLeft_Right_DividerItemDecoration(this);
    }

    protected boolean hasItemDecoration()
    {
        return true;
    }

    protected final RecyclerView getRecyclerView()
    {
        return recyclerView;
    }

    protected final Adapter getAdapter()
    {
        return recyclerAdapter;
    }

    protected final void setRefresh(boolean isDownRefresh)
    {
        this.isDownRefresh = isDownRefresh;
    }

    public void setDisableRefresh(boolean refresh)
    {
        getxRefreshView().setEnableRefresh(refresh);
    }

    public void setDisableLoadMore(boolean loadMore)
    {
        getxRefreshView().setEnableLoadMore(loadMore);
    }

    public void setDisableRefreshAndLoadMore(boolean disable)
    {
        setDisableRefresh(disable);
        setDisableLoadMore(disable);
    }

    @Override
    public void noMoreData()
    {
        super.noMoreData();
        getxRefreshView().setEnableLoadMore(false);
    }

    @Override
    public void loadMoreError()
    {
        super.loadMoreError();
        getxRefreshView().finishLoadMore();
    }
}
